package helper;

import java.util.Properties;

import dao.DSN;
/**
 * Methods for handling DSNs.
 * @author Jolle
 * @version 1.0.0
 */
public class DSNHelper {
	
	public static DSN getDSNfromProperties(Properties props) {
		String host = "";
		String port = "";
		String user = "";
		String pass = "";
		if (props.containsKey("host")) {
			host = props.getProperty("host");
		}
		if (props.containsKey("port")) {
			port = props.getProperty("port");
		}
		if (props.containsKey("user")) {
			user = props.getProperty("user");
		}
		if (props.containsKey("pass")) {
			pass = props.getProperty("pass");
		}
		return new DSN(host, port, user, pass);
	}
	
	public static String getPropertiesInfo(Properties props) {
		StringBuilder info = new StringBuilder();
		info.append("  host:");
		if (props.containsKey("host")) {
			info.append(props.getProperty("host"));
		}
		info.append("  port:");
		if (props.containsKey("port")) {
			info.append(props.getProperty("port"));
		}
		info.append("  user:");
		if (props.containsKey("user")) {
			info.append(props.getProperty("user"));
		}
		info.append("  pass:");
		if (props.containsKey("pass")) {
			info.append(props.getProperty("pass"));
		}
		info.append("  type:");
		if (props.containsKey("type")) {
			info.append(props.getProperty("type"));
		}
		else {
			info.append("(na)");
		}
		info.append("  dbname:");
		if (props.containsKey("dbname")) {
			info.append(props.getProperty("dbname"));
		}
		else {
			info.append("(na)");
		}
		info.append("\n  dbpath:");
		if (props.containsKey("dbpath")) {
			info.append(props.getProperty("dbpath"));
		}
		else {
			info.append("(na)");
		}
		return info.toString();
	}
}
